#include <iostream>
#include <unordered_map>
#include <string>
#include <queue>
using namespace std;
char s[10];

int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1}; 

int bfs()
{
    string end("12345678x");
    unordered_map<string, int> d;
    queue<string> q;
    q.push(s);
    while(q.size())
    {
        auto cur = q.front(); q.pop();
        if(cur == end) return d[cur];
        int d_cur = d[cur];
        int pos = cur.find('x'), x = pos / 3, y = pos % 3;
        for(int i = 0; i < 4; ++i)
        {
            int r = x + dx[i], c = y + dy[i];
            if(r < 0 || c < 0 || r >= 3 || c >= 3) continue;
            swap(cur[r * 3 + c], cur[pos]);
            if(!d.count(cur))
            {
                d[cur] = d_cur + 1;
                q.push(cur);
            }
            swap(cur[r * 3 + c], cur[pos]);
        }
    }
    return -1;
}

int main()
{
    for(int i = 0; i < 9; ++i) cin >> s[i];
    
    cout << bfs();
    
    return 0;
}